<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta name="viewport"
        content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no" />
    <title>首页</title>
    <link rel="stylesheet" type="text/css" href="css/api.css" />
    <link rel="stylesheet" href="css/main.css" />
</head>

<body>
    <div id="app">
        <script src="script/flexible.js"></script>
        <script src="script/api.js"></script>
        <script src="script/vue.min.js"></script>
        <script src="script/fastclick.js"></script>
        <script src="script/restful.js"></script>
        <script src="script/common.js"></script>
        <script src="script/businessCommon.js"></script>
        <script>
            apiready = function () {
                var NVTabBar = api.require('NVTabBar');
                var vm = new Vue({
                    el: '#app',
                    data: {
                        headerH: 0,
                        footerH: 0,
                        frameIndex: 0,
                        isCart: false,
                        isRecommend: true,
                        frames: [{
                            name: 'home_win',
                            url: 'html/home/home_win.html',
                        }, {
                            name: 'circle_win',
                            url: 'html/circle_new/circle_home.html',
                        }, {
                            name: 'release_win',
                            url: 'html/release/release_win.html',
                        }, {
                            name: 'mine_win',
                            url: 'html/mine/mine_win.html',
                        }],
                        items: [{
                            w: api.winWidth / 4,
                            bg: {
                                marginB: -2,
                                image: '#fff'
                            },
                            iconRect: {
                                w: 22,
                                h: 22,
                            },
                            icon: {
                                normal: 'widget://image/footer/home.png',
                                selected: 'widget://image/footer/home_on.png',
                            },
                            title: {
                                text: '首页',
                                size: 12.0,
                                normal: '#999999',
                                selected: '#C7000B',
                                marginB: 2.0
                            }
                        }, {
                            w: api.winWidth / 4,
                            bg: {
                                marginB: -2,
                                image: '#fff'
                            },
                            iconRect: {
                                w: 22,
                                h: 22,
                            },
                            icon: {
                                normal: 'widget://image/footer/circle.png',
                                selected: 'widget://image/footer/circle_on.png'
                            },
                            title: {
                                text: '发现',
                                size: 12.0,
                                normal: '#999999',
                                selected: '#C7000B',
                                marginB: 2.0
                            }
                        }, {
                            w: api.winWidth / 4,
                            bg: {
                                marginB: -2,
                                image: '#fff'
                            },
                            iconRect: {
                                w: 22,
                                h: 22,
                            },
                            icon: {
                                normal: 'widget://image/footer/release.png',
                                selected: 'widget://image/footer/release_on.png'
                            },
                            title: {
                                text: '发布',
                                size: 12,
                                normal: '#999999',
                                selected: '#C7000B',
                                marginB: 2
                            }
                        }, {
                            w: api.winWidth / 4,
                            bg: {
                                marginB: -2,
                                image: '#fff'
                            },
                            iconRect: {
                                w: 22,
                                h: 22,
                            },
                            icon: {
                                normal: 'widget://image/footer/mine.png',
                                selected: 'widget://image/footer/mine_on.png'
                            },
                            title: {
                                text: '我的',
                                size: 12.0,
                                normal: '#999999',
                                selected: '#C7000B',
                                marginB: 2.0
                            }
                        }]
                    },
                    created: function () {
                    },
                    mounted: function () {
                        const agreePrivacy = $api.getStorage('agreePrivacy');
                        if (!agreePrivacy) {
                            api.openWin({
                                name: 'priv_win',
                                url: './html/common/priv_win.html',
                                bounces: false,
                                pageParam: { key: 'value' }
                            });
                        } else {
                            this.getSystemSet()
                        }
                    },
                    methods: {
                        //百度统计注册
                        registerBaidu() {
                            if (api.systemType == "android") {
                                // android平台的初始化
                                var demo = api.require('modulebaidumtj');
                                demo.startWithAppkey({
                                    appkey: '90e2f7af39',
                                    appVersion: api.appVersion,
                                    channelId: 'AndroidChannel',
                                    enableExceptionLog: 'true',
                                    enableDebugOn: 'false'
                                });
                            } else {
                                // iOS平台的初始化
                                var demo = api.require('modulebaidumtj');
                                demo.startWithAppkey({
                                    appkey: '470ea418c5',
                                    appVersion: api.appVersion,
                                    channelId: 'iOSChannel',
                                    enableExceptionLog: 'true',
                                    enableDebugOn: 'true'
                                });
                            }
                        },
                        //注册极光推送
                        registerJGpush() {
                            //极光推送初始化
                            var ajpush = api.require('ajpush');
                            ajpush.init(function (ret) {
                                if (ret && ret.status) {
                                    console.log(JSON.stringify(ret))
                                    //success
                                }
                            });
                            ajpush.setBadge({ badge: 0 });
                            // ajpush.getRegistrationId(function (ret) {
                            //     var registrationId = ret.id;

                            // }); 

                            //ios前台监听
                            if (api.systemType == 'ios') {
                                ajpush.setListener(
                                    function (ret) {
                                        var extra = ret.extra;
                                        if (extra.url) {
                                            api.openWin({
                                                name: 'web_win',
                                                url: './html/web/web_win.html',
                                                bounces: false,
                                                pageParam: {
                                                    url: extra.url
                                                }
                                            });
                                        }
                                    }
                                );
                            }
                        },
                        // 底部导航模块
                        openNVTabBar(items) {
                            var vm = this;
                            NVTabBar.open({
                                styles: {
                                    bg: '#fff',
                                    h: 50,
                                    dividingLine: {
                                        width: 0,
                                        color: '#f7f7f7'
                                    }
                                },
                                items: items,
                                selectedIndex: 0
                            }, function (ret, err) {
                                if (ret) {
                                    if (ret.eventType == "click") {
                                        vm.setFrameGroupIndex(ret.index)
                                    }
                                }
                            });
                        },
                        getSystemSet() {
                            var vm = this;
                            common('getSystemSet')({}, function (ret) {
                                console.log("系统设置：" + JSON.stringify(ret))
                                vm.openTab(ret.data);
                                vm.registerBaidu()
                                vm.registerJGpush()
                            }, function (ret, err) {
                                vm.openTab({});
                                vm.registerBaidu()
                                vm.registerJGpush()
                            })
                        },
                        openTab(config) {
                            var vm = this;
                            vm.isCart = config.onlineBuyEnabled;
                            // if (vm.isCart) {
                            //     vm.frames = [{
                            //         name: 'home_win',
                            //         url: 'html/home/home_win.html',
                            //     }, {
                            //         name: 'circle_win',
                            //         url: 'html/circle_new/circle_hone.html',
                            //     }, {
                            //         name: 'release_win',
                            //         url: 'html/release/release_win.html',
                            //     }, {
                            //         name: 'cart_win',
                            //         url: 'html/store/cart_win.html',
                            //         bounces: true,
                            //     }, {
                            //         name: 'mine_win',
                            //         url: 'html/mine/mine_win.html',
                            //     }]
                            //     vm.items = [{
                            //         w: api.winWidth / 5.0,
                            //         bg: {
                            //             marginB: -2,
                            //             image: '#fff'
                            //         },
                            //         iconRect: {
                            //             w: 22,
                            //             h: 22,
                            //         },
                            //         icon: {
                            //             normal: 'widget://image/footer/home.png',
                            //             selected: 'widget://image/footer/home_on.png',
                            //         },
                            //         title: {
                            //             text: '首页',
                            //             size: 12.0,
                            //             normal: '#696969',
                            //             selected: '#3F8CEC',
                            //             marginB: 2
                            //         }
                            //     }, {
                            //         w: api.winWidth / 5.0,
                            //         bg: {
                            //             marginB: -2,
                            //             image: '#fff'
                            //         },
                            //         iconRect: {
                            //             w: 22,
                            //             h: 22,
                            //         },
                            //         icon: {
                            //             normal: 'widget://image/footer/circle.png',
                            //             selected: 'widget://image/footer/circle_on.png'
                            //         },
                            //         title: {
                            //             text: '发现',
                            //             size: 12.0,
                            //             normal: '#696969',
                            //             selected: '#3F8CEC',
                            //             marginB: 2
                            //         }
                            //     }, {
                            //         w: api.winWidth / 5.0,
                            //         bg: {
                            //             marginB: api.systemType == 'ios' ? 23 : 8,
                            //             image: 'rgba(0,0,0,0)'
                            //         },
                            //         iconRect: {
                            //             w: 50,
                            //             h: 50,
                            //         },
                            //         icon: {
                            //             normal: 'widget://image/footer/add.png',
                            //             selected: 'widget://image/footer/add.png',
                            //         },
                            //         title: {
                            //             text: '发布',
                            //             size: 12,
                            //             normal: '#696969',
                            //             selected: '#3F8CEC',
                            //             marginB: 2
                            //         }
                            //     }, {
                            //         w: api.winWidth / 5.0,
                            //         bg: {
                            //             marginB: -2,
                            //             image: '#fff'
                            //         },
                            //         iconRect: {
                            //             w: 22,
                            //             h: 22,
                            //         },
                            //         icon: {
                            //             normal: 'widget://image/footer/cart.png',
                            //             selected: 'widget://image/footer/cart_on.png'
                            //         },
                            //         title: {
                            //             text: '购物车',
                            //             size: 12.0,
                            //             normal: '#696969',
                            //             selected: '#3F8CEC',
                            //             marginB: 2
                            //         }
                            //     }, {
                            //         w: api.winWidth / 5.0,
                            //         bg: {
                            //             marginB: -2,
                            //             image: '#fff'
                            //         },
                            //         iconRect: {
                            //             w: 22,
                            //             h: 22,
                            //         },
                            //         icon: {
                            //             normal: 'widget://image/footer/mine.png',
                            //             selected: 'widget://image/footer/mine_on.png'
                            //         },
                            //         title: {
                            //             text: '我的',
                            //             size: 12.0,
                            //             normal: '#696969',
                            //             selected: '#3F8CEC',
                            //             marginB: 2
                            //         }
                            //     }]
                            // }
                            vm.openNVTabBar(vm.items)
                            setLightStatusBar();
                            vm.openFrameGroup();
                            $api.setStorage('systemSet', config);
                        },
                        openFrameGroup() {
                            var vm = this;
                            var footerHeight = 65
                            if (api.deviceModel.indexOf('iPhone') > -1) {
                                var deviceModel = api.deviceModel.split(',')[0].replace('iPhone', '')
                                if (deviceModel >= 10) {
                                    footerHeight = 100
                                }
                            }
                            vm.frames.map((item) => {
                                item.pageParam = {
                                    footerH: vm.footerH,
                                    footerHeight: footerHeight
                                }
                            })
                            api.openFrameGroup({
                                name: 'home_frame_group',
                                rect: {
                                    marginTop: vm.headerH,
                                    marginBottom: footerHeight,
                                    w: 'auto'
                                },
                                scrollEnabled: false,
                                frames: vm.frames,
                                preload: 0
                            }, function (ret, err) {
                                vm.frameIndex = ret.index;
                                if (vm.frameIndex == 0) {
                                    api.sendEvent({ name: 'refreshUnReadMessageNum', extra: { page: 'home' } })
                                }
                            });
                        },
                        setFrameGroupIndex(frameIndex) {

                            if (vm.isCart && frameIndex === 3) {
                                if (!checkToken()) return false;
                            }
                            if (frameIndex === 2) {
                                if (!checkToken()) return false;
                            }
                            api.setFrameGroupIndex({
                                name: 'home_frame_group',
                                index: frameIndex,
                            });
                            vm.frameIndex = frameIndex;
                            // if (vm.isCart) {
                            // if (frameIndex > 1) {
                            //     api.sendEvent({
                            //         name: 'login'
                            //     });
                            // }
                            // api.setFrameAttr({
                            //     name: 'mine_frm',
                            //     hidden: frameIndex != 4
                            // });
                            // } else {
                            if (frameIndex > 1) {
                                api.sendEvent({
                                    name: 'login'
                                });
                            }
                            api.setFrameAttr({
                                name: 'mine_frm',
                                hidden: frameIndex != 3
                            });
                            // }
                            api.setFrameGroupAttr({
                                name: 'release_win',
                                hidden: frameIndex != 2
                            });
                            api.setFrameGroupAttr({
                                name: 'circle_group',
                                hidden: frameIndex != 1
                            });
                            api.setFrameGroupAttr({
                                name: 'circle_recommend_group',
                                hidden: frameIndex != 1 || !vm.isRecommend
                            });
                            api.setFrameAttr({
                                name: 'home_frm',
                                hidden: frameIndex != 0
                            });
                        },
                    }
                })
                api.addEventListener({
                    name: 'keyback'
                }, function (ret, err) {
                    api.sendEvent({
                        name: 'eventCircleClosePhoto',
                        extra: {
                            key: ' value'
                        }
                    });
                    var time_last = $api.getStorage('time_last') || 0;
                    var time_now = Date.parse(new Date());
                    if (time_now - time_last > 2000) {
                        $api.setStorage('time_last', time_now);
                        api.toast({
                            msg: '再按一次退出应用',
                            duration: 2000,
                            location: 'bottom'
                        });
                    } else {
                        api.closeWidget({
                            silent: true
                        });
                    }
                });

                api.addEventListener({
                    name: 'eventTab'
                }, function (ret, err) {
                    NVTabBar.bringToFront();
                });

                api.addEventListener({
                    name: 'sendAgreePrivacy'
                }, function (ret, err) {
                    vm.getSystemSet()
                });

                api.addEventListener({
                    name: 'eventCircleChange'
                }, function (ret, err) {
                    vm.isRecommend = ret.value.value == 0
                });

                api.addEventListener({
                    name: 'closeWin'
                }, function (ret, err) {
                    const winName = ret.value.name;
                    const delay = ret.value.delay;
                    if (winName) {
                        console.log(winName)
                        setTimeout(function () {
                            api.closeWin({
                                name: winName
                            });
                        }, delay ? delay : 1000);
                    }
                });

                //android 极光推送监听
                api.addEventListener({
                    name: 'appintent'
                }, function (ret, err) {
                    if (ret && ret.appParam.ajpush) {
                        var ajpush = ret.appParam.ajpush;
                        if (ajpush.extra.url) {
                            api.openWin({
                                name: 'web_win',
                                url: './html/web/web_win.html',
                                bounces: false,
                                pageParam: {
                                    url: ajpush.extra.url
                                }
                            });
                        }
                    }
                })
                //ios 在后台极光推送监听
                api.addEventListener({
                    name: 'noticeclicked'
                }, function (ret, err) {
                    if (ret && ret.value) {
                        var ajpush = ret.value;
                        if (ajpush.extra.url) {
                            api.openWin({
                                name: 'web_win',
                                url: './html/web/web_win.html',
                                bounces: false,
                                pageParam: {
                                    url: ajpush.extra.url
                                }
                            });
                        }
                    }
                })
            }
        </script>
    </div>
</body>

</html>